/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client.bean;

import client.dal.AccountDAL;
import client.dal.CustomerDAL;
import client.entity.Account;
import client.entity.Customer;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import util.EncryptionPassword;

/**
 *
 * @author mrlucky
 */
@ManagedBean
@SessionScoped
public class CustomerSignUp implements java.io.Serializable {

    private Account acc;
    private Customer cus;
    private String confirmPass;
    private String pass_temp;

    /**
     * Creates a new instance of CustomerSignUp
     */
    public CustomerSignUp() {
    }

    public String getPass_temp() {
        return pass_temp;
    }

    public void setPass_temp(String pass_temp) {
        this.pass_temp = pass_temp;
    }

    public Account getAcc() {
        if (acc == null) {
            acc = new Account();
        }
        return acc;
    }

    public void setAcc(Account acc) {
        this.acc = acc;
    }

    public String getConfirmPass() {
        return confirmPass;
    }

    public void setConfirmPass(String confirmPass) {
        this.confirmPass = confirmPass;
    }

    public Customer getCus() {
        if (cus == null) {
            cus = new Customer();
        }
        return cus;
    }

    public void setCus(Customer cus) {
        this.cus = cus;
    }

    public String insertCustomerSignUp() {
        if (new AccountDAL().loadAccountByUserName(acc.getUseName()) == null) {
            String hashPass = new EncryptionPassword().Encryption(acc.getPassword());
            Account a = new Account(acc.getUseName(), hashPass, Boolean.TRUE);
            Customer c = new Customer(a, cus.getName(), cus.getAddress(), cus.getPhone(), null, cus.getDescription());
            new AccountDAL().insertAccount(a);
            new CustomerDAL().insertCustomer(c);
            pass_temp = "";
            for (int i = 0; i < acc.getPassword().length(); i++) {
                pass_temp += "*";
            }
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("account", a);
            return "customersignupsuccess.xhtml?faces-redirect=true";
        }
        else{
            FacesContext.getCurrentInstance().addMessage("form1:useName", new FacesMessage("* User name is used"));
            return "";
        }

    }

    public String signUpSuccess() {
        Account a = new AccountDAL().loadAccountByUserName(acc.getUseName());
        //return "createProfileStep1.xhtml?accId="+accID+"&faces-redirect=true";
        return "createProfileStep1.xhtml?accId=" + a.getId() + "&faces-redirect=true";
    }
}
